package cn.buk.tms.eotms.controller;

import cn.buk.common.JsonResult;
import cn.buk.common.dto.CommonDto;
import cn.buk.tms.eotms.request.HotelOrderRequestDto;
import cn.buk.tms.eotms.response.HotelOrderResponseDto;
import cn.buk.tms.eotms.sc.HotelOrderSearchCriteria;
import cn.buk.tms.eotms.service.HotelOrderService;
import java.util.List;
import jakarta.servlet.http.HttpServletRequest;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

/**
 * @author yfdai
 */
@RestController
@RequestMapping("/hotel")
public class HotelOrderController extends BaseController {

  @Autowired
  public HotelOrderController(HotelOrderService hotelOrderService) {
    this.hotelOrderService = hotelOrderService;
  }

  private final HotelOrderService hotelOrderService;

  /**
   * 搜索酒店订单
   * @param request
   * @param sc
   * @return
   */
  @RequestMapping("/orders")
  public CommonDto<HotelOrderResponseDto> searchHotelOrders(HttpServletRequest request,
      @ModelAttribute("sc") HotelOrderSearchCriteria sc) {

    List<HotelOrderResponseDto> datas = hotelOrderService.searchHotelOrders(getUserId(request), sc);

    CommonDto<HotelOrderResponseDto> commonDto = new CommonDto<>();
    commonDto.setDataList(datas);
    commonDto.setPage(sc.getPage());

    return commonDto;
  }

  /**
   * 获取酒店订单详情
   * @param request
   * @return
   */
  @RequestMapping("/order/{orderId}")
  public HotelOrderResponseDto searchHotelOrders(HttpServletRequest request,
                                                 @PathVariable("orderId") final int orderId) {
    return hotelOrderService.getHotelOrderById(getUserId(request), orderId);
  }

  /**
   * 创建酒店订单
   * @param request
   * @param dto
   * @return
   */
  @RequestMapping(value = "/order", consumes = "application/json", method = RequestMethod.POST)
  public JsonResult createHotelOrder(HttpServletRequest request,
                                     @RequestBody HotelOrderRequestDto dto) {
    return hotelOrderService.createHotelOrder(getUserId(request), dto);
  }

  /**
   * 取消订单
   * @param request
   * @param orderId
   * @param remark
   * @return
   */
  @RequestMapping(value = "/order/{orderId}/status/canceled", method = RequestMethod.POST)
  public JsonResult cancelHotelOrder(HttpServletRequest request,
                                     @PathVariable("orderId") final int orderId,
                                     @RequestParam("remark") String remark) {
    return hotelOrderService.cancelHotelOrder(getUserId(request), orderId, remark);
  }

  /**
   * 提交酒店订单
   */
  @RequestMapping(value = "/order/{orderId}/status/submitted", method = RequestMethod.POST)
  public JsonResult submitHotelOrder(HttpServletRequest request,
                                     @PathVariable("orderId") int orderId
  ) {
    return hotelOrderService.submitHotelOrder(getUserId(request), orderId);
  }

  /**
   * 审批订单
   * @param request
   * @param orderId
   * @param denyCode 0-同意；其它-不同意
   * @param denyReason 不同意的原因，不同意时必填
   * @return
   */
  @RequestMapping(value = "/order/{id}/status/approved", method = RequestMethod.POST)
  public JsonResult approveFlightOrder(HttpServletRequest request,
                                       @PathVariable("id") int orderId,
                                       @RequestParam("denyCode") int denyCode,
                                       @RequestParam(value = "denyReason", required = false) String denyReason
  ) {
    return hotelOrderService.approveHotelOrder(getUserId(request), orderId, denyCode, denyReason);
  }

  /**
   * 为酒店订单付款
   */
  @PostMapping(value = "/order/{id}/pay")
  public JsonResult payHotelOrder(HttpServletRequest request,
                                   @PathVariable("id") int id
  ) {
    return hotelOrderService.payHotelOrder(getUserId(request), id);
  }
}
